# Maintainer: Sarah Ottinger

_realname=click
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=8.1.6
pkgrel=1
pkgdesc='A simple wrapper around optparse for powerful command line utilities (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://click.palletsprojects.com/"
license=('spdx:BSD-3-Clause')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-colorama"
)
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python-pallets-sphinx-themes"
  "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-log-cabinet"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx-issues"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx-tabs"
)
source=("${_realname}-$pkgver.tar.gz::https://github.com/pallets/click/archive/${pkgver}.tar.gz")
sha512sums=('ae31e4b5666b7ab6638a12fd12088c5b5311a4f6913e3471aefdf2392cf47d4be6e745e09070d6f2e382fa92c4068c196e0109295d86953dd066f7619dfbd4cd')

prepare() {
  rm -rf "python-build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build

  # Forward installation for building documentation
  _pkgdir="${srcdir}/python-install-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${_pkgdir}" --optimize=1 --skip-build

  msg "Build documentation"
  cd "${srcdir}/python-build-${MSYSTEM}/docs"
  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
  PYTHONPATH="${_pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages" \
    make html
}

package_python-click() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE.rst "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.rst"
}

package_python-click-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}"
  cp -a "${srcdir}/python-build-${MSYSTEM}/docs/_build/html" "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/html"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
